const raseAccount = {
    raseData: {},
    taskConfig: { taskStatus: false },
    raseAccountData: {
        start_datetime: '',
        end_datetime: "",
        second: "",
        look_post_num: 0,
        like_post_num: 0,
        comment_post_num: 0,
    },
    viewPostList: [],
    initOtherData() {
        //读取缓存
        if(Xhs.cacheTask.cacheData){
            raseAccount.raseAccountData = JSON.parse(JSON.stringify(Xhs.cacheTask.cacheData))
            return
        }
        raseAccount.raseAccountData.viewPostList = []
        raseAccount.raseAccountData = {
            start_datetime: '',
            end_datetime: "",
            second: "",
            look_post_num: 0,
            like_post_num: 0,
            comment_post_num: 0,
        }
    },
    watchValue(key, newVal) {
        if (key == 'taskStatus' && newVal) {
            if (newVal !== true) {
                //当前浏览任务已全部完成
                raseAccount.raseAccountData.end_datetime = newVal
                raseAccount.raseAccountData.second = (newVal - raseAccount.raseAccountData.start_datetime) / 1000
                sleep(2000)
                console.log('养号任务结束----------------------------');
                console.log('raseAccountData', JSON.stringify(raseAccount.raseAccountData));
                console.log('viewPostList', JSON.stringify(raseAccount.viewPostList));
            }
            //关闭缓存功能
            Xhs.watchCache = false;
            Xhs.cacheTask = {
                task:{
                    taskName: null,
                    taskFunc: null,
                    taskData: null,
                },
                taskId:null,
                cacheData: null
            }
            sleep(2000)
            Xhs.taskConfig.taskStatus = true
        }
        if (key == 'errorTask' && newVal) {
            let params = {
                taskType: 'rase_account',
                taskName: '养号',
                taskData: raseAccount.raseData,
                errorTip: newVal,
                taskDate: Comman.getDate()
            }
            Xhs.errorTask(params)
            raseAccount.taskConfig.taskStatus = true
        }

    },
    init(data) {
        //开启缓存任务中
        // Xhs.watchCache = true;
        Xhs.cacheTask.taskId = data.task_id;
        Xhs.isHome = true
        raseAccount.initOtherData()
        raseAccount.raseData = data;
        raseAccount.taskConfig = Comman.defineReactive(
            {
                taskStatus: false,
                errorTask: '',  
            },
            '',
            raseAccount.watchValue
        )
        console.log('初始化raseAccountData：', JSON.stringify(raseAccount.raseAccountData));
        console.log('初始化viewPostList：', JSON.stringify(raseAccount.viewPostList));
        sleep(1000)
        // Xhs.getXhsTask()
        raseAccount.openTask()
    },
    start(data) {
        //初始化数据
        raseAccount.init(data)
    },
    openTask() {
        let setting = raseAccount.raseData.setting
        let homeMnue = btnConfig.home.homeMnue
        let homeBtn = btnConfig.home.homeBtn
        let searchIconBtn = btnConfig.home.searchIconBtn
        let searchBtn = btnConfig.search.searchBtn

        //重置首页
        while (!depth(homeMnue.options.depth).id(homeMnue.options.id).exists()) {
            back();
            console.log('back');
            sleep(2000);
        }
        //点击首页
        let nowHomeBtn = homeBtn.default()
        if (!nowHomeBtn) {
            console.log('首页按钮不存在，请检查并手动复原首页重新启动');
            raseAccount.taskConfig.errorTask = '首页按钮不存在'
            return
        }
        if (nowHomeBtn.bounds) {
            App.clickByBtn(nowHomeBtn, 3)
        } else {
            App.click(homeBtn.position.x, homeBtn.position.y, 3)
        }
        //点击搜索图标按钮
        let nowSearchIconBtn = searchIconBtn.default()
        if (!nowSearchIconBtn) {
            console.log('搜索图片不存在，并手动复原重新启动');
            raseAccount.taskConfig.errorTask = '搜索图片不存在'
            return
        }
        if (nowSearchIconBtn.bounds) {
            App.clickByBtn(nowSearchIconBtn, 3)
        } else {
            App.click(searchIconBtn.position.x, searchIconBtn.position.y, 3)
        }
        //输入关键字搜索
        setText(Comman.getRandomArrayItem(setting.keywords))
        sleep(2000)

        //点击搜索按钮
        let nowSearchBtn = searchBtn.default()
        if (!nowSearchBtn) {
            console.log('搜索按钮不存在，请检查并手动复原重新启动');
            raseAccount.taskConfig.errorTask = '搜索按钮不存在'
            return
        }
        if (nowSearchBtn.bounds) {
            App.clickByBtn(nowSearchBtn, 3)
        } else {
            App.click(searchBtn.position.x, searchBtn.position.y, 3)
        }
        //开启自动浏览任务
        raseAccount.raseAccountData.start_datetime = new Date().getTime()
        raseAccount.startTask(setting)
    },
    startTask(setting) {
        sleep(2000)
        let endTime = raseAccount.raseData.time[1] * 1000 - 30000
        let nowTime1 = new Date().getTime()
        let postList = btnConfig.search.postList.default()
        let postBtn = btnConfig.search.postBtn.default()
        console.log('活动结束日期：', new Date(endTime).toLocaleDateString() + new Date(endTime).toLocaleTimeString());
        if (postBtn.empty()) {
            console.log('未找到帖子列表，请检查');
            raseAccount.taskConfig.errorTask = '未找到帖子列表'
            return;
        }
        if (endTime <= nowTime1) {
            //任务已结束
            console.log('已到任务结束时间');
            if (!raseAccount.taskConfig.taskStatus) {
                raseAccount.taskConfig.taskStatus = nowTime1
            }
            return;
        }
        console.log('当前页面的所有笔记：', postBtn.length);
        postBtn.forEach((item, index) => {
            let nowTime = new Date().getTime()
            if (endTime <= nowTime) {
                //任务已结束
                if (!raseAccount.taskConfig.taskStatus) {
                    raseAccount.taskConfig.taskStatus = nowTime
                }
                return;
            }
            let text = item.findOne(id('g8r'))
            if(!text || !text.text) {
                return
            }
            console.log(index + '：正在浏览：' + text.text());
            if (raseAccount.viewPostList.findIndex(view => view == text.text()) == -1) {
                //未浏览的笔记
                raseAccount.raseAccountData.look_post_num += 1
                raseAccount.viewPostList.push(text.text())
                raseAccount.resolveItem(item, setting)
            }
        })
        //遍历后翻页
        if (postList) {
            //翻页新笔记
            let nowTime = new Date().getTime()
            if (endTime > nowTime) {
                console.log('遍历后翻页');
                postList.scrollForward()
                //翻页新笔记
                raseAccount.startTask(setting)
            } else {
                if (!raseAccount.taskConfig.taskStatus) {
                    raseAccount.taskConfig.taskStatus = nowTime
                }
            }
        }
    },
    resolveItem(item, setting) {
        App.clickByBtn(item, 3)
        // App.randomSleep()
        gestures([350, [300, 440], [300, 400]]);
        App.randomSleep()
        //点赞概率
        let like = Comman.percenValue(setting.like_ratio)
        let likeBtn = btnConfig.post.likeBtn
        //关注概率
        let follow = Comman.percenValue(setting.follow_ratio)

        //留言概率
        let resolve = false && Comman.percenValue(setting.comment_ratio)
        let resolveBtn = btnConfig.post.resolveBtn
        let resolveInputBtn = btnConfig.post.resolveInputBtn
        console.log('like：', like, 'follow：', follow, 'resolve：', resolve)

        // 滑动概率
        let scroll = Comman.percenValue(50)
        if (scroll) {
            gestures([350, [300, 440], [300, 400]]);
        }
        App.randomSleep()

        // 点赞
        if (like) {
            let nowLikeBtn = likeBtn.default()
            if (nowLikeBtn) {
                if (nowLikeBtn.bounds) {
                    App.clickByBtn(nowLikeBtn, true)
                } else {
                    App.click(likeBtn.position.x, likeBtn.position.y, true)
                }
                raseAccount.raseAccountData.like_post_num += 1
            } else {
                console.log('未找到点赞按钮')
            }
        }
        // 评论
        if (resolve) {
            let nowResolveBtn = resolveBtn.default()
            let nowResolveInputBtn = resolveInputBtn.default()
            //有评论按钮和输入框才执行留言操作
            if (nowResolveBtn && nowResolveInputBtn) {
                if (nowResolveInputBtn.bounds) {
                    App.clickByBtn(nowResolveInputBtn, true)
                } else {
                    App.click(resolveInputBtn.position.x, resolveInputBtn.position.y, true)
                }
                if (setting.leave_content && setting.leave_content.length) {
                    setText(Comman.getRandomArrayItem(setting.leave_content))
                }

                let sendBtn = btnConfig.post.infoSendBtn.default() || btnConfig.post.infoSendBtn2.default()
                if (sendBtn) {
                    sleep(2000)
                    if (!Xhs.isTest) {
                        if (sendBtn.bounds) {
                            App.clickByBtn(sendBtn, true)
                        } else {
                            App.click(btnConfig.post.infoSendBtn.position.x, btnConfig.post.infoSendBtn.position.y, true)
                        }
                    } else {
                        console.log('测试阶段-可以发送评论');
                        back()
                        sleep(2000)
                    }
                    raseAccount.raseAccountData.comment_post_num += 1
                } else {
                    console.log('未找到发送按钮')
                }
            } else {
                console.log('未找到评论按钮')
            }
        }
        // 关注
        if (follow) {
            raseAccount.followUser(setting)
        }
        App.randomSleep()
        let options = btnConfig.search.postList.options
        while (!depth(options.depth).id(options.id).exists()) {
            back();
            console.log('back...');
            sleep(2000);
        }
    },
    followUser(setting) {
        let followBtn = btnConfig.post.followBtn
        let followBtn2 = btnConfig.post.followBtn2
        let followVideoBtn = btnConfig.post.followVideoBtn
        //点击关注按钮
        let nowFollowBtn = followBtn.default() || followBtn2.default() || followVideoBtn.default()
        if (nowFollowBtn) {
            if (nowFollowBtn.bounds) {
                App.clickByBtn(nowFollowBtn, true)
            } else {
                App.click(followBtn.position.x || followVideoBtn.position.x, followBtn.position.y || followVideoBtn.position.y, true)
            }
            sleep(2000)
            // 点击用户头像
            followImgBtn = btnConfig.post.followImgBtn.default() || btnConfig.post.followVideoImgBtn.default() || btnConfig.post.followVideoImgBtn2.default()
            if (followImgBtn) {
                if (followImgBtn.bounds) {
                    App.clickByBtn(followImgBtn, true)
                } else {
                    App.click(btnConfig.post.followImgBtn.position.x, btnConfig.post.followImgBtn.position.y, true)
                }
                sendMessageBtn = btnConfig.user.sendMessageBtn.default() || btnConfig.user.sendMessageBtnFollow.default()
                if (sendMessageBtn) {
                    if (sendMessageBtn.bounds) {
                        App.clickByBtn(sendMessageBtn, true)
                    } else {
                        App.click(btnConfig.user.sendMessageBtn.position.x, btnConfig.user.sendMessageBtn.position.y, true)
                    }
                    // 设置发送内容
                    setText(setting.follow_words)
                    sleep(2000)
                    // 点击发送按钮
                    let sendBtn = btnConfig.message.sendMessageBtn.default()
                    if (sendBtn) {
                        if (!Xhs.isTest) {
                            if (sendBtn.bounds) {
                                App.clickByBtn(sendBtn, true)
                            } else {
                                App.click(btnConfig.message.sendMessageBtn.position.x, btnConfig.message.sendMessageBtn.position.y, true)
                            }
                        } else {
                            console.log('测试阶段-可以发送私信');
                        }
                    } else {
                        console.log('未找到发送按钮');
                    }
                } else {
                    console.log('未找到发送私信按钮')
                }
            } else {
                console.log('未找到用户头像按钮');
            }
        } else {
            console.log('未找到关注按钮')
        }
    }
}
module.exports = raseAccount